package com.kaili.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.client.service.ILoginUserInfoService;
import com.kaili.common.exception.BaseExcept;
import com.kaili.common.util.SnowflakeIdFactory;
import com.kaili.db.service.IHsOptEwbAbnormalService;
import com.kaili.db.service.IHsOptEwbService;
import com.kaili.except.ResponseStatus;
import com.kaili.pojo.dto.abnormal.EwbAbnormalAddDTO;
import com.kaili.pojo.dto.abnormal.EwbAbnormalDTO;
import com.kaili.pojo.entity.HsOptEwbAbnormal;
import com.kaili.pojo.vo.ArriveEwbVO;
import com.kaili.pojo.vo.ewbAbnormalVO;
import com.kaili.utils.DateUtils;
import com.kaili.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.beanutils.BeanMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @AUTHOR pengdongxu.
 * @Date: 18-5-18 下午3:59.
 * @COPYRIGHT 杭州凯立
 */
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "到件异常",description = "到件异常服务API")
@RestController
public class EwbAbnormalController {


    @Autowired
    private MessageSource messageSource;

    @Autowired
    private IHsOptEwbAbnormalService hsOptEwbAbnormalService;
    @Autowired
    ILoginUserInfoService loginUserInfoService;
    @Autowired
    IHsOptEwbService hsOptEwbService;


    //@PostMapping("/createdEwbAbnormalBS")
    @PostMapping("/ewbAbnormals/BS")
    @ApiOperation(value = "新增异常件B/S")
    public ResponseStatus createdEwbAbnormalBS(@RequestBody EwbAbnormalAddDTO req) {
       /*//检验单号是否存在
       HsOptEwb ewb = hsOptEwbService.getEWbByEwbNo(req.getEwbNo());
       if (ewb == null){
           return ResponseStatus.EXCEPTION_ADD_NONE;
       }*/
       //重复申报校验
       ewbAbnormalVO abnormal = hsOptEwbAbnormalService.queryAbnormalByEwbNo(req);
       if (abnormal != null){
           return ResponseStatus.RESPONSE_CHECK_ERROR;
       }
       try {
           String operationId = SnowflakeIdFactory.getNextId();
           hsOptEwbAbnormalService.createdEwbAbnormalBS(req);
       }catch (Exception e){
           e.printStackTrace();
           throw new  BaseExcept(ResponseStatus.EXCEPTION_ADD_ERR);
       }
       return ResponseStatus.RESPONSE_OK;
    }

    @GetMapping("/ewbAbnormals")
    @ApiOperation(value = "分页查询到货异常数据")
    Page<ewbAbnormalVO> queryewbAbnormalList(@ModelAttribute EwbAbnormalDTO req) {
        if (req.getCur() != null && req.getPageSize() != null) {
            Page<ewbAbnormalVO> pager = new Page<ewbAbnormalVO>(req.getCur(), req.getPageSize());

            Map<Object, Object> map = new HashMap<>();
            map.putAll(new BeanMap(req));
            pager = hsOptEwbAbnormalService.queryAbnormal(pager, map);
            return pager;
        }
        throw new BaseExcept(ResponseStatus.TRANS_SEND_ERR);
    }

    @DeleteMapping("/ewbAbnormals/{operationId}")
    @ApiOperation(value ="删除到货异常单")
    ResponseStatus deleteAbnormalByEwbNo(@PathVariable("operationId") @ApiParam(value = "操作编号", defaultValue = "0000011111") String operationId) {
        HsOptEwbAbnormal hsOptEwbAbnormal = new HsOptEwbAbnormal();
        hsOptEwbAbnormal.setOperationId(operationId);
        hsOptEwbAbnormal.setModifiedBy(loginUserInfoService.getLoginUserId());
        hsOptEwbAbnormal.setModifiedTime(DateUtils.getNowDate());
        try {
            hsOptEwbAbnormalService.deleteAbnormalByEwb(hsOptEwbAbnormal);
        }catch (Exception e){
            throw new BaseExcept(ResponseStatus.DELETE_ERR);
        }
        return ResponseStatus.RESPONSE_OK;
    }


    @PutMapping(value = "/ewbAbnormals/{ewbNo}")
    @ApiOperation(value = "修改到货异常单")
    ResponseStatus modifyAbnormal(@RequestBody EwbAbnormalAddDTO ewbAbnormalDTO) {
        try {

            hsOptEwbAbnormalService.modifyAbnormal(ewbAbnormalDTO);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseStatus.MODIFIED_ABNORMAL_ERR;
        }
        return ResponseStatus.RESPONSE_OK;
    }

    @GetMapping("/ewbAbnormals/{ewbNo}/ewbs")
    @ApiOperation(value = "获取原始运单信息", notes = "获取原始运单信息")
    List<ArriveEwbVO> queryOriginalEwb(@PathVariable("ewbNo") @ApiParam(value = "运单号", defaultValue = "0000011111") String ewbNo) {
        Map<String ,Object> map = new HashMap<>();
        map.put("ewbNo",ewbNo);
        return hsOptEwbAbnormalService.queryOriginalEwb(map);
    }


   /* @PostMapping("/ewbAbnormals/CS")
    @ApiOperation(value = "新增异常件C/S")
    List<Map<String, Object>> createdEwbAbnormalCS(@RequestBody List<EwbAbnormalAddDTO> req) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < req.size(); i++) {
            EwbAbnormalAddDTO ewbAbnormalAddDTO = req.get(i);
            ResponseStatus responseStatus = createdEwbAbnormalBS(ewbAbnormalAddDTO);
            HashMap<String, Object> map = new HashMap<>();
            map.put("message", messageSource.getMessage(responseStatus.getMsg(), null, CommonUtil.getLocale()));
            map.put("status", responseStatus.getCode());
            map.put("ewbNo", ewbAbnormalAddDTO.getEwbNo());
            list.add(map);
        }
        return list;
    }*/
}
